class IncidentsController < ApplicationController
  before_filter :check_authentication, :except => [:show, :index, :showByMachine, :showByTypeIncident]
  #require 'FasterCSV'
  layout 'layout'
 
  # GET /incidents
  # GET /incidents.xml

  def index
    @incidents = Incident.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @incidents }
    end
  end

  # GET /incidents/1
  # GET /incidents/1.xml
  def show
    @incident = Incident.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @incident }
    end
  end

  # GET /incidents/new
  # GET /incidents/new.xml
  def new
    @incident = Incident.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @incident }
    end
  end

  # GET /incidents/1/edit
  def edit
    @incident = Incident.find(params[:id])
  end

  # POST /incidents
  # POST /incidents.xml
  def create
    @incident = Incident.new(params[:incident])

    respond_to do |format|
      if @incident.save
        flash[:notice] = 'Incident créé avec succès'
        sendmail
        format.html { redirect_to(@incident) }
        format.xml  { render :xml => @incident, :status => :created, :location => @incident }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /incidents/1
  # PUT /incidents/1.xml
  def update
    @incident = Incident.find(params[:id])

    respond_to do |format|
      if @incident.update_attributes(params[:incident])
        flash[:notice] = 'Incident a été modifié avec succès'
        addTolog(current_user.id,"L'incident " + @incident.id.to_s + " a été modifié.")
        format.html { redirect_to(@incident) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /incidents/1
  # DELETE /incidents/1.xml
  def destroy
    @incident = Incident.find(params[:id])
    @incident.destroy

    respond_to do |format|
      format.html { redirect_to(incidents_url) }
      format.xml  { head :ok }
    end
  end
  
  def showByMachine
    @machine = Machine.find(params[:id])
    @incidents = Incident.find_all_by_machine_id(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @incidents }
    end
  end
  
  def showByTypeIncident
    @incidents = Incident.find_all_by_type_incident_id(params[:id])
    @TypeIncident = TypeIncident.find_by_id(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @incidents }
    end
  end
  
    def export_to_csv
    addTolog(current_user.id,"creation du csv")
    @incidents = Incident.find(:all) 
    csv_string = FasterCSV.generate do |csv| 
      # header row 
      csv << ["id", "date_detection", "machine_id", "type_incident_id", "date_resolution", "created_at", "updated_at"] 
   
      # data rows 
      @incidents.each do |inc| 
        csv << [inc.id, inc.date_detection, inc.type_incident_id, inc.date_resolution, inc.created_at, inc.updated_at] 
      end 
    end 
   
    # send it to the browsah
    time = Time.new
    timestring = time.strftime("%Y%m%d %H%M%S")
    dispositionstring = "attachment; filename=" + timestring + "_logs.csv" 
    send_data csv_string, 
              :type => 'text/csv; charset=iso-8859-1; header=present', 
              :disposition => dispositionstring
  end
  



  
end
